﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using NDK.Module.Model;
using NDK.Motion.MachineManager;
using NDK.Utils;

namespace NDK.Frm
{
    public partial class Dlg_SelectProject : Form
    {
        public string Title { get; set; } = "项目选择";

        public Dlg_SelectProject()
        {
            InitializeComponent();
            //this.Width = 300;
            //this.Height = 180;
            this.StartPosition = FormStartPosition.Manual;
            int x = SystemInformation.PrimaryMonitorSize.Width / 2 - this.Width / 2;
            int y = SystemInformation.PrimaryMonitorSize.Height / 2 - this.Height / 2;
            this.Location = new Point(x, y);
        }

        #region 引入窗体动画效果方法
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
        #endregion

        private void lbl_Ok_Click(object sender, EventArgs e)
        {
            if (cb_SelectProject.Text != "请选择产品")
            {
                Common.ProjectName = cb_SelectProject.Text;
                MachineDataManager.ProjectName = cb_SelectProject.Text;
                this.DialogResult = DialogResult.OK;
                Common.IsOfflineMode = CB_Offline.Checked;
            }
        }

        private void lbl_Ok_MouseEnter(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Ok, true);
        }

        private void lbl_Ok_MouseLeave(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Ok, false);
        }

        private void Dlg_SelectProject_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lbl_Ok_Click(sender, e);
            }
        }

        private void cb_SelectProject_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lbl_Ok_Click(sender, e);
            }
        }

        private void Dlg_SelectProject_Load(object sender, EventArgs e)
        {
            //this.Text = Title;
            string[] dirNames = FileUtils.GetDirectories(MachineDataManager.dirPath);
            cb_SelectProject.Items.Clear();
            if (dirNames.Length==0)
            {
                return;
            }
            foreach (string fileName in dirNames)
            {
                if (!fileName.Contains('\\'))
                {
                    continue;
                }
                string projectName = fileName.Split('\\').ToList().Last();
                if(projectName.Contains (Common.SP_线体) && !projectName.Contains("副本"))
                {
                    cb_SelectProject.Items.Add(projectName);
                    if(cb_SelectProject.Items.Count >0)
                    {
                        cb_SelectProject.SelectedIndex = 0;
                    }
                  
                }
            }
        }
    }
}
